<?php

/*
 * 发红包
 */
namespace App\Http\Controllers\Property;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Common\json;
class  LottoController extends Controller{

    //按小区来获取注册人数
    public function allquantity($village_id){
        $data       = DB::table('user') ->where('user_village','=',$village_id) ->count();
        return $data;
    }

    //判断是否点点击过了
    public function selectcollar($id){
        $data         = DB::table('user') ->where('user_id',$id)->first();
        return $data;
    }

    //没有点击的时候的接口

    public function NotClick(Request $request){
        if($request    ->isMethod('post')){
            $user_id           = $request ->input('user_id');
            $village_id        = $request ->input('village_id');
            $sum               = $this    ->allquantity($village_id);   //按小区注册人数统计
            $mapphonesum       = $this    ->mapphonesum($user_id,$village_id);      //按用户来统计邀请人数
            $mapphone          = $this    ->mapphone($user_id,$village_id);         //获取邀请人的手机号
            $data              = array('sum' =>$sum,'mapphonesum'=>$mapphonesum,'mapphone'=>$mapphone);
            return json::json('success', '获取抽奖参数成功', $data);
        }
    }

    //全部人都可以抽奖
    public function total($id,$village){
        $select       = DB::table('lotto')->where('lotto_village',$village)->first();
        $total    = $select -> money;
        $rand   = rand(0*10,3*10)/10;
        $hasdraw         = $this ->collar($village);
        $selectcollar    = $this ->selectcollar($id);
        $mapphonesum   = $this   ->mapphonesum($id,$village);
        $mapphone      = $this   ->mapphone($id,$village);
        if($total == 0){
            DB::table('user')->where('user_id', $id)->update(['usermoney' => 0,'time'=>1]);
            DB::table('lotto')->where('lotto_id',1)->update(['money' => 0]);
            $da        = array('money' =>'0','hasdraw'=>$hasdraw,'invite' =>$mapphonesum,'mapphone' => $mapphone);
            return json::json('success', '获取详情成功', $da);
        }
        if($rand >$total){
            DB::table('user')->where('user_id', $id)->update(['usermoney' => $total,'time'=>1]);
            DB::table('lotto')->where('lotto_id',1)->update(['money' => 0]);
            $da        = array('money' =>$total,'hasdraw'=>$hasdraw,'invite' =>$mapphonesum,'mapphone' => $mapphone);
            return json::json('success', '获取详情成功', $da);
        }

        if(empty($selectcollar ->usermoney)){
            $tot = $total - $rand;
            DB::table('user')->where('user_id', $id)->update(['usermoney' => $rand,'time'=>1]);
            DB::table('lotto')->where('lotto_id',1)->update(['money' => $tot]);
            $da        = array('money' =>$rand,'hasdraw'=>$hasdraw,'invite' =>$mapphonesum,'mapphone' => $mapphone);
            return json::json('success', '获取详情成功', $da);
        }else{
            $arr = (object)array();
            return json::json('failure', '您也抽过奖', $arr);
        }


    }


    //获取领过用户数量
    public function collar($village){
        $data      = DB::table('user') ->where('user_village','=',$village) ->whereNotNull('usermoney') ->count();
        return $data;
    }

    //统计领取金额数
    public function sum(){
        $data     = DB::table('user') ->sum('usermoney');
        return $data;
    }

    public function first($id,$village){
        $select         = DB::table('user') ->select('phone') ->where('user_id',$id)
            ->where('user_village','=',$village) ->first();
            return $select;
    }

    //获取用户邀请人的手机号
    public function mapphone($id,$village){
        $select        = $this ->first($id,$village);
        if($select     == null){
            return   array();
        }else {
            $data = DB::table('user')->select('phone')->where('user_invitation', $select->phone)
                ->where('user_village', '=', $village)
                ->get();
            if ($data == true) {
                $dd = $data;
            } else {
                $dd = [];
            }
            return $dd;
        }

    }
    //统计邀请好友数
    public function mapphonesum($id,$village){
        $select        = $this ->first($id,$village);
        if($select     == null){
            return 0;
        }else {
            $data = DB::table('user')->select('phone')->where('user_invitation', $select->phone)
                ->where('user_village', '=', $village)
                ->count();
            return $data;
        }
    }

    //抽奖规则
    public function lottery(Request $request){
        if($request  ->isMethod('post')){
            $id            = $request ->input('user_id');
            $village_id    = $request ->input('village_id');
            return $this->total($id,$village_id);
        }
    }

    //抽奖前判断
    public function judge(Request $request){
        if($request ->isMethod('post')){
            $arr   = (object)array();
            $user_id          = $request ->input('user_id');
            $village_id    = $request ->input('village_id');
            $data             = DB::table('user') ->where('user_id',$user_id)->get();
            if($data  == true){
                foreach ($data as $value){
                  //  $values            = $value ->usermoney;
                    $time             = $value ->time;
                    $usermoney        = $value->usermoney;
                }
                $money   = array('money'=>$usermoney);
                if(empty($time)) {
                    return json::json('success', '你有一次抽奖机会', $arr);
                }elseif ($time ==1){
                    return json::json('success', '你已经抽过奖了', $money);
                }elseif ($time ==2){
                    $hasdraw         = $this ->collar($village_id);
                    $mapphonesum   = $this   ->mapphonesum($user_id,$village_id);
                    $mapphone      = $this   ->mapphone($user_id,$village_id);
                    $data         = DB::table('user') ->where('time',2)->first();
                    $da        = array('phone' => $data->phone,'hasdraw'=>$hasdraw,'invite' =>$mapphonesum,'mapphone' => $mapphone,'money'=>$data->usermoney,'time'=>2);
                    return json::json('failure', '恭喜您中奖了', $da);
                }
            }else{
                return json::json('failure', '对不起你查找的用户不存在', $arr);
            }
            /*$value            = $data ->usermoney;
            $time             = $data ->time;
            if(is_null($value) && is_null($time)){
                return json::json('success', '你次抽奖机会', $arr);
            }elseif ($time ==2){
                $hasdraw         = $this ->collar($village_id);
                $mapphonesum   = $this   ->mapphonesum($user_id,$village_id);
                $mapphone      = $this   ->mapphone($user_id,$village_id);
                $data         = DB::table('user') ->where('time',2)->first();
                $da        = array('phone' => $data->phone,'hasdraw'=>$hasdraw,'invite' =>$mapphonesum,'mapphone' => $mapphone);
                return json::json('failure', '恭喜中奖', $da);
            }*/
        }
    }


}